﻿using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using System.Text;
using Warehouse.Api.Read.AllCation.Command;
using Warehouse.Api.Read.Dtos;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Read.AllCation.Hanlder
{
    public class GetProductCommandHanlder : IRequestHandler<GetProductCommand, ApiPaging<ProductDto>>
    {
        public readonly IBaseRepostity<Stocking> _baseRepostity;
        public readonly ILogger<GetProductCommandHanlder> logger;

        public GetProductCommandHanlder(IBaseRepostity<Stocking> baseRepostity, ILogger<GetProductCommandHanlder> logger)
        {
            _baseRepostity = baseRepostity;
            this.logger = logger;
        }
        /// <summary>
        /// 出库信息的显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<ApiPaging<ProductDto>> Handle(GetProductCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ProductDto> result = new ApiPaging<ProductDto>();
            try
            {
                var countSql = new StringBuilder();
                var sqlBuilder = new StringBuilder();
                sqlBuilder.Append(@"SELECT 
                                   stok.id,
                                   stok.AssociatedOrderNumber,
                                   pro.ProductName,
                                   pro.ProductCode,
                                   stok.Bacth,
                                   stok.Price,
                                   pro.Specifications,
                                   stok.IncomingQuantity,
                                   li.LocationName,
                                   sup.SupplierName,
                                   sup.SupplierContact,
                                   stok.Totalprice,
                                   stok.IncomingQuantity
                                   FROM Stocking stok
                                   JOIN ProductModel pro 
                                    ON  stok.ProductId = pro.ProductId
                                   JOIN SupplierModel sup
                                   ON stok.SupplierId = sup.SupplierId
                                    JOIN  LocationModel li 
                                  ON stok.StorageId = li.LocationId
                                  WHERE IsDeleted =  false
                                    ");
                countSql.Append(@" SELECT COUNT(stok.id) FROM Stocking stok
                                   JOIN ProductModel pro 
                                    ON  stok.ProductId = pro.ProductId
                                   JOIN SupplierModel sup
                                   ON stok.SupplierId = sup.SupplierId
                                    JOIN  LocationModel li 
                                  ON stok.StorageId = li.LocationId
                                  WHERE IsDeleted =  false");
                var parmanters = new DynamicParameters();
                sqlBuilder.Append("LIMIT @PageSize OFFSET @OFFSET");
                parmanters.Add("PageSize", request.PageSize);
                parmanters.Add("OFFSET", (request.PageIndex - 1) * request.PageSize);
                using var conn = new MySqlConnection("server=10.223.8.23;uid=root;pwd=123456;database=warehousemanagement ");
                await conn.OpenAsync(cancellationToken);
                var data = await conn.QueryAsync<ProductDto>(sqlBuilder.ToString(), parmanters);
                var total = await conn.ExecuteScalarAsync<int>(countSql.ToString(), parmanters);
                if (request.PageSize <= 0)
                {
                    result.Msg = "显示数量不能小于等于0";
                    result.Code = ApiEume.Error;
                    return result;
                }
                result.Msg = "显示成功";
                result.Code = ApiEume.Success;
                result.PageData = data.ToList();
                result.TotaCount = total;
                result.PageCount = (int)Math.Ceiling((double)total / request.PageSize);
            }
            catch (Exception ex)
            {

            }
            return  result;
        }
    }
}
